package tehUtils;

import net.minecraft.src.Block;
import net.minecraft.src.EntityItem;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.World;

public class TehInventory implements IInventory {
	
	private ItemStack[] mainInventory = new ItemStack[]{};
	public boolean inventoryChanged;
	public static int inventorySize = 4;

	private int getInventorySlotContainItem(int i) {
		for (int j = 0; j < mainInventory.length; j++)
			if (mainInventory[j] != null && mainInventory[j].itemID == i)
				return j;

		return -1;
	}

	private int getInventorySlotContainItemAndDamage(int i, int j) {
		for (int k = 0; k < mainInventory.length; k++)
			if (mainInventory[k] != null && mainInventory[k].itemID == i
				&& mainInventory[k].getItemDamage() == j)
				return k;

		return -1;
	}

	private int storeItemStack(ItemStack itemstack) {
		for (int i = 0; i < mainInventory.length; i++)
			if (mainInventory[i] != null
				&& mainInventory[i].itemID == itemstack.itemID
				&& mainInventory[i].isStackable()
				&& mainInventory[i].stackSize < mainInventory[i].getMaxStackSize()
				&& mainInventory[i].stackSize < getInventoryStackLimit()
				&& (!mainInventory[i].getHasSubtypes() 
				|| mainInventory[i].getItemDamage() == itemstack.getItemDamage())
				&& ItemStack.func_46154_a(mainInventory[i], itemstack))
				return i;

		return -1;
	}

	private int getFirstEmptyStack() {
		for (int i = 0; i < mainInventory.length; i++)
			if (mainInventory[i] == null)
				return i;

		return -1;
	}

	private int storePartialItemStack(ItemStack itemstack) {
		int i = itemstack.itemID;
		int j = itemstack.stackSize;

		if (itemstack.getMaxStackSize() == 1) {
			int k = getFirstEmptyStack();
			if (k < 0)
				return j;
			if (mainInventory[k] == null)
				mainInventory[k] = ItemStack.copyItemStack(itemstack);
			return 0;
		}

		int l = storeItemStack(itemstack);
		if (l < 0)
			l = getFirstEmptyStack();
		if (l < 0)
			return j;

		if (mainInventory[l] == null) {
			mainInventory[l] = new ItemStack(i, 0, itemstack.getItemDamage());
			if (itemstack.hasTagCompound())
				mainInventory[l].setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
		}

		int i1 = j;
		if (i1 > mainInventory[l].getMaxStackSize() - mainInventory[l].stackSize)
			i1 = mainInventory[l].getMaxStackSize() - mainInventory[l].stackSize;
		if (i1 > getInventoryStackLimit() - mainInventory[l].stackSize)
			i1 = getInventoryStackLimit() - mainInventory[l].stackSize;

		if (i1 == 0)
			return j;
		else {
			j -= i1;
			mainInventory[l].stackSize += i1;
			mainInventory[l].animationsToGo = 5;
			return j;
		}
	}

	public boolean consumeInventoryItem(int i) {
		int j = getInventorySlotContainItem(i);
		if (j < 0)
			return false;
		if (--mainInventory[j].stackSize <= 0)
			mainInventory[j] = null;
		return true;
	}

	public boolean hasItem(int i) {
		int j = getInventorySlotContainItem(i);
		return j >= 0;
	}

	public boolean addItemStackToInventory(ItemStack itemstack) {
		if (!itemstack.isItemDamaged()) {
			int i;
			do {
				i = itemstack.stackSize;
				itemstack.stackSize = storePartialItemStack(itemstack);
			} while (itemstack.stackSize > 0 && itemstack.stackSize < i);
			if (itemstack.stackSize == i) {
				itemstack.stackSize = 0;
				return true;
			} else
				return false;
		}
		int j = getFirstEmptyStack();
		if (j >= 0) {
			mainInventory[j] = ItemStack.copyItemStack(itemstack);
			mainInventory[j].animationsToGo = 5;
			itemstack.stackSize = 0;
			return true;
		}

		return false;
	}

	public ItemStack decrStackSize(int i, int j) {
		if (mainInventory[i] != null) {
			if (mainInventory[i].stackSize <= j) {
				ItemStack itemstack = mainInventory[i];
				mainInventory[i] = null;
				return itemstack;
			}
			ItemStack itemstack1 = mainInventory[i].splitStack(j);
			if (mainInventory[i].stackSize == 0)
				mainInventory[i] = null;
			return itemstack1;
		} else
			return null;
	}

	public void setInventorySlotContents(int i, ItemStack itemstack) {
		mainInventory[i] = itemstack;
	}

	public NBTTagList writeToNBT(NBTTagList nbttaglist) {
		for (int i = 0; i < mainInventory.length; i++)
			if (mainInventory[i] != null) {
				NBTTagCompound nbttagcompound = new NBTTagCompound();
				nbttagcompound.setByte("Slot", (byte) i);
				mainInventory[i].writeToNBT(nbttagcompound);
				nbttaglist.appendTag(nbttagcompound);
			}
		return nbttaglist;
	}

	public void readFromNBT(NBTTagList nbttaglist) {
		mainInventory = new ItemStack[36];
		for (int i = 0; i < nbttaglist.tagCount(); i++) {
			NBTTagCompound nbttagcompound = (NBTTagCompound) nbttaglist.tagAt(i);
			int j = nbttagcompound.getByte("Slot") & 0xff;
			ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound);
			if (itemstack == null)
				continue;
			if (j >= 0 && j < mainInventory.length)
				mainInventory[j] = itemstack;
		}
	}

	public int getSizeInventory() {
		return mainInventory.length;
	}

	public ItemStack getStackInSlot(int i) {
		return mainInventory[i];
	}

	public String getInvName() {
		return "Teh Inventory";
	}

	public int getInventoryStackLimit() {
		return 64;
	}

	public void onInventoryChanged() {
		inventoryChanged = true;
	}

	public boolean hasItemStack(ItemStack itemstack) {
		for (int j = 0; j < mainInventory.length; j++)
			if (mainInventory[j] != null
				&& mainInventory[j].isStackEqual(itemstack))
				return true;

		return false;
	}

	public void openChest() {
	}

	public void closeChest() {
	}
	
	public boolean hasRoomForItem(ItemStack item) {
		if (getFirstEmptyStack() != -1)
			return true;
		for (int i=0; i < inventorySize; i++) {
			if (mainInventory[i].getItem() == item.getItem() 
				&& mainInventory[i].getMaxStackSize() - mainInventory[i].stackSize <= item.stackSize)
				return true;
		}
		return false;
	}

	public boolean isUseableByPlayer(EntityPlayer entityplayer) {
		return false;
	}

}
